package com.zhangpan.uaa.dao;

import com.zhangpan.uaa.entity.PermissionDto;
import com.zhangpan.uaa.entity.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDao {


    @Autowired
    JdbcTemplate jdbcTemplate;

    public UserDto getUserByUsername(String username){

        String sql ="select id,username,password,fullname from t_user where username = ?";
        List<UserDto> list = jdbcTemplate.query(sql,new Object[]{username},new BeanPropertyRowMapper<>(UserDto.class));
        if (list.isEmpty() && list.size() <=0){
            return null;
        }

        System.out.println("----->"+list.get(0));

        return list.get(0);
    }

    //根据ID查询用户权限
    public List<String> findPermissionByUserId(String userId){
        String sql = "SELECT * FROM t_permission WHERE id in (SELECT role_id FROM t_user_role WHERE user_id=?)";
        List<PermissionDto> list = jdbcTemplate.query(sql,new Object[]{userId},new BeanPropertyRowMapper<>(PermissionDto.class));
        List<String> permissions = new ArrayList<>();
        list.iterator().forEachRemaining(c->permissions.add(c.getCode()));
        return permissions;

    }
}
